<?php

namespace App\Http\Service;

use App\Util\Result;
use App\Model\Member;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

trait ServiceMember{

    /**
     * @return mixed
     * 用户列表
     * czs
     */
    public static function getList($limit){
        $model = DB::table('readers');
        $list = $model->where('is_main',1)->orderBy('id','desc');

        $res['count'] = $list->count();
        $res['list'] = $list
            ->paginate($limit);
        return Result::success()->setData($res);
    }

    /**
     *获取单个id记录
     * czs
     */
    public static function getOne($id) {
        $res = Member::where("id", $id)->first();
        return Result::success()->setData($res);
    }

    /**
     *更新记录
     * czs
     */
    public static function updateData($input, $id) {
        $res = Member::where("id", $id)->update($input);
        return Result::success()->setData($res);
    }

    /**
     * 禁用、启用
     * czs
     */
    public static function able($id,$able) {
        Member::where("id", $id)->update(['status' => $able]);
        if ($able==2) {
            return Result::success()->setMsg("启用")->setData(2);
        } else {
            return Result::success()->setMsg("禁用")->setData(1);
        }
    }

    /**
     *web更新个人信息
     * czs
     */
    public static function updateInfo($input) {
        $id = $input["id"];
        $input['birthday'] = strtotime($input['birthday']);
        unset($input["id"]);
        $res = Member::where("id", $id)->update($input);
        if ($res) {
            $member = Member::find($id);
            session(['member'=>$member]);
        }
        return Result::success()->setData($res);
    }

    /**
     * web注册
     * czs
     */
    public static function register($input) {
        $code = Cache::get($input['phone']);
        if ($code!=$input['yzm']) {
            return Result::failure()->setMsg("验证码不正确！")->setType(1);
        }
        if ($input['password']!=$input['qrpassword']) {
            return Result::failure()->setMsg("密码和确认密码不一致！")->setType(2);
        }
        $sel = DB::table('members')->where('phone',$input['phone'])->count();
        if ($sel) {
            return Result::failure()->setMsg("该手机号已被注册！")->setType(3);
        }
        $data['account'] = $input['phone'];
        $data['phone'] = $input['phone'];
        $data['password'] = bcrypt($input['password']);
        $data['status'] = 1;
        $res = DB::table('members')->insert($data);
        return Result::success()->setData($res);
    }

    /**
     * 找回密码
     * czs
     */
    public static function getPassword($input) {
        $code = Cache::get($input['telnum']);
        if ($code!=$input['yznum']) {
            return Result::failure()->setMsg("验证码不正确！")->setType(1);
        }
        if ($input['new_pwd']!=$input['confirm_pwd']) {
            return Result::failure()->setMsg("密码和确认密码不一致！")->setType(2);
        }
        $data['password'] = bcrypt($input['new_pwd']);
        $res = DB::table('members')->where('phone',$input['telnum'])->update($data);
        return Result::success()->setData($res);
    }

    /**
     * 更换头像
     * czs
     */
    public static function changeHead($memberId,$head) {
        $res = Member::where('id',$memberId)->update(['head'=>$head]);
        return Result::success()->setData($res);
    }

    /**
     * wap用户信息
     * czs
     */
    public static function memberCenter($openid) {
        $model = Member::where('openid',$openid);
        $res = $model->get();
        if (empty($res[0]->avatar)) {
            $avatar = session('wechat_user')['original']['headimgurl'];
            $model->update(['avatar'=>$avatar]);
            $res = $model->get();
        }
        return Result::success()->setData($res[0]);
    }

    /**
     * wap更改用户信息
     * czs
     */
    public static function saveInfo($input,$memberId) {
        $input['birthday'] = strtotime($input['birthday']);
        Member::where('id',$memberId)->update($input);
    }

    /**
     * wap绑定手机号
     * czs
     */
    public static function wapBindTel($post,$openid,$avatar) {
//        $code = Cache::get($post['phone']);
//        if ($code!=$post['code']) {
//            return Result::failure()->setMsg("验证码不正确！");
//        }
        $sel = Member::where('phone',$post['phone'])->where('openid',$openid)->count();
        if ($sel) {
            return Result::failure()->setMsg("已有帐号绑定此手机号！");
        }
        $sel2 = Member::where('openid',$openid);
        $data['phone'] = $post['phone'];
        $data['avatar'] = $avatar;
        if ($sel2->count()&&empty($sel2->value('phone'))) {
            $sel2->update($data);
            $member = $sel2->first();
            session(['member'=>$member]);
            return Result::success()->setMsg("绑定成功");
        } else {
            $data['account'] = $post['phone'];
            $data['openid'] = $openid;
            $data['status'] = 1;
            Member::create($data);
            $member = Member::where('phone',$post['phone'])->first();
            session(['member'=>$member]);
            return Result::success()->setMsg("绑定成功");
        }
    }

    /**
     * web绑定手机号
     * czs
     */
    public static function webBindTel($post,$openid,$unionid,$avatar) {
        $code = Cache::get($post['phone']);
        if ($code!=$post['code']) {
            return Result::failure()->setMsg("验证码不正确！");
        }
        if ($post['mm']!=$post['qrmm']) {
            return Result::failure()->setMsg("密码和确认密码不一致！")->setType(2);
        }
        $sel = Member::where('phone',$post['phone'])->where('openid',$openid)->count();
        if ($sel) {
            return Result::failure()->setMsg("已有帐号绑定此手机号！");
        }
        $sel2 = Member::where('phone',$post['phone'])->where('pc_openid',Null);
        $sel3 = Member::where('phone',$post['phone']);
        $data['pc_openid'] = $openid;
        $data['unionid'] = $unionid;
        $data['avatar'] = $avatar;
        $data['password'] = bcrypt($post['mm']);
        if ($sel2->count()) {
            $sel2->update($data);
            $member = Member::where('phone',$post['phone'])->first();
            session(['member'=>$member]);
            return Result::success()->setMsg("绑定成功");
        } elseif ($sel3->count()) {
            return Result::failure()->setMsg("已有帐号绑定此手机号！");
        } else {
            $data['account'] = $post['phone'];
            $data['phone'] = $post['phone'];
            $data['status'] = 1;
            Member::create($data);
            $member = Member::where('phone',$post['phone'])->first();
            session(['member'=>$member]);
            return Result::success()->setMsg("绑定成功");
        }
    }

    /**
     * web更换手机号
     * czs
     */
    public static function webChangeTel($post,$memberId) {
        $code = Cache::get($post['phone']);
        if ($code!=$post['code']) {
            return Result::failure()->setMsg("验证码不正确！");
        }
        $data['phone'] = $post['phone'];
        $update = Member::where('id',$memberId)->update($data);
        if ($update) {
            $member = Member::where('phone',$post['phone'])->first();
            session(['member'=>$member]);
            return Result::success();
        } else {
            return Result::failure()->setMsg('操作失败！');
        }

    }
}


